//
//  XQPublicIdentityBirthdayPopView.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/2/7.
//

import UIKit

class XQPublicIdentityBirthdayPopView: UIView {
    
    var sureHandle: ((String) -> Void)?
    
    var cancelHandle: (() -> Void)?
    
    var selectY = ""
    var selectM = ""
    var selectD = ""
    
    private lazy var contentView : UIView = {
        let v = UIView()
        return v
    }()
    
    private lazy var bgImageView : UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "pop_base_bg")
        imageView.isUserInteractionEnabled = true
        return imageView
    }()
    
    private lazy var sureBtn : UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("OK", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.backgroundColor = .mainTextColor
        btn.layer.cornerRadius = 24
        btn.addTarget(self, action: #selector(sureClick), for: .touchUpInside)
        return btn
    }()
    
    private lazy var calcelBtn : UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("Close", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.layer.cornerRadius = 24
        btn.layer.borderWidth = 1
        btn.layer.borderColor = UIColor.white.cgColor
        btn.addTarget(self, action: #selector(cancelClick), for: .touchUpInside)
        return btn
    }()
    
    private lazy var titleLb : UILabel = {
        let lb = UILabel()
        lb.text = "Select date"
        lb.textColor = .white
        lb.font = UIFont.systemFont(ofSize: 18, weight: .medium)
        return lb
    }()
    
    private lazy var yearView : XQBirthdayChooseView = {
        let yearView = XQBirthdayChooseView()
        yearView.dataSource = XQCalendarTool.getYearDataSourceWithMinYear()
        yearView.selectDateHandle = {[weak self] date in
            guard let srongify = self else {return}
            srongify.selectY = date
            srongify.updateChooseTime()
        }
        return yearView
    }()
    
    private lazy var monthView : XQBirthdayChooseView = {
        let monthView = XQBirthdayChooseView()
        monthView.dataSource = XQCalendarTool.getMonthDataSource()
        monthView.selectDateHandle = {[weak self] date in
            guard let srongify = self else {return}
            srongify.selectM = date
            srongify.updateChooseTime()
        }
        return monthView
    }()
    
    private lazy var dayView : XQBirthdayChooseView = {
        let dayView = XQBirthdayChooseView()
        dayView.selectDateHandle = {[weak self] date in
            guard let srongify = self else {return}
            srongify.selectD = date
        }
        return dayView
    }()
    
    private lazy var timeView : UIView = {
        let view = UIView()
        return view
    }()
    
    private lazy var middleView : UIView = {
        let view = UIView()
        view.backgroundColor = .mainTextColor
        return view
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.isUserInteractionEnabled = true
        makeUI()
        makeConstraints()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        makeUI()
        makeConstraints()
    }
    
    private func makeUI() {
        self.addSubview(self.contentView)
        self.contentView.addSubview(self.bgImageView)
        self.contentView.addSubview(self.sureBtn)
        self.contentView.addSubview(self.calcelBtn)
        self.bgImageView.addSubview(self.titleLb)
        self.bgImageView.addSubview(self.timeView)
        self.timeView.addSubview(self.middleView)
        self.timeView.addSubview(self.yearView)
        self.timeView.addSubview(self.monthView)
        self.timeView.addSubview(self.dayView)
        
    }
    
    private func makeConstraints() {
        
        let width = (UIDevice.xq_width() - 32) / 3
        
        self.contentView.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        self.bgImageView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.width.equalTo(UIDevice.xq_width() - 32)
            make.height.equalTo(420)
        }
        self.sureBtn.snp.makeConstraints { make in
            make.top.equalTo(self.bgImageView.snp.bottom).offset(30)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(48)
        }
        self.calcelBtn.snp.makeConstraints { make in
            make.top.equalTo(self.sureBtn.snp.bottom).offset(15)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(48)
            make.bottom.equalTo(0)
        }
        self.titleLb.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(54)
        }
        self.timeView.snp.makeConstraints { make in
            make.top.equalTo(self.titleLb.snp.bottom).offset(24)
            make.left.right.equalTo(0)
            make.width.equalTo(UIDevice.xq_width() - 32)
            make.height.equalTo(5 * 46)
        }
        self.middleView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.left.right.equalTo(0)
            make.height.equalTo(46)
        }
        self.dayView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.width.equalTo(width)
            make.bottom.equalTo(0)
        }
        self.yearView.snp.makeConstraints { make in
            make.top.equalTo(self.dayView.snp.top)
            make.bottom.equalTo(self.dayView.snp.bottom)
            make.right.equalTo(0)
            make.width.equalTo(width)
        }
        self.monthView.snp.makeConstraints { make in
            make.top.equalTo(self.dayView.snp.top)
            make.left.equalTo(self.dayView.snp.right)
            make.right.equalTo(self.yearView.snp.left)
            make.bottom.equalTo(self.dayView.snp.bottom)
        }
    }
    
    func setSelectData(date : String?) {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy/M/d"
        
        let dd : Date?
        if (isValidDate(date ?? "", format:" yyyy/M/d")) {
            dd = dateFormatter.date(from: date!)
        }else {
            dd = dateFormatter.date(from:"1970/1/1")
        }
        
       
        
        let calendar = Calendar.current
        let components = calendar.dateComponents([.year, .month, .day], from: dd!)
        
        let year : Int = components.year!
        let month : Int = components.month!
        let day : Int = components.day!
        
        selectY = "\(year)"
        selectM = "\(month)"
        selectD = "\(day)"
        
        self.yearView.selectData = selectY
        self.monthView.selectData = XQCalendarTool.getMonthWithIndex(index: month)
        self.dayView.dataSource = XQCalendarTool.getDay(year: year, month: month)
        self.dayView.selectData = selectD
    }
    
    private func updateChooseTime() {
        let yInt = Int(self.selectY)
        let MInt = Int(self.selectM)
        self.dayView.dataSource = XQCalendarTool.getDay(year: yInt!, month: MInt!)
    }
    
    private func isValidDate(_ dateString: String, format: String) -> Bool {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = format
        dateFormatter.locale = Locale(identifier: "en_US_POSIX") // 设置为固定的 locale
        dateFormatter.timeZone = TimeZone(secondsFromGMT: 0) // 设置为 UTC 时区
        return dateFormatter.date(from: dateString) != nil
    }
    
    @objc private func sureClick() {
        let date = "\(selectY)-\(selectM)-\(selectD)"
        sureHandle?(date)
    }
    
    @objc private func cancelClick() {
        cancelHandle?()
    }
    
}
